<?php
namespace app\common\model;

use think\Model;

class Order extends Model
{
    /**
     * @param array $where
     * @param string $fields
     * @return array|bool|false|\PDOStatement|string|Model
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findOne(array $where=[], $fields='o.*,u.name,u.tel')
    {
        $info = $this
            ->alias('o')
            ->join('__USER__ u', 'u.id=o.uid')
            ->where($where)
            ->field($fields)
            ->find();
        if (empty($info)) {
            return false;
        }
        $info = $info->toArray();
        $info['goods_list'] = db('order_goods')->where(['order_id'=>$info['id']])->select();
        return $info;
    }

    /**
     * @param array $where
     * @param bool $pageSize
     * @param array $pageconfig
     * @param bool $fields
     * @param string $order
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList(array $where=[], $pageSize=false, array $pageconfig=[], $fields=false, $order='o.id desc')
    {
        return $this
            ->alias('o')
            ->join('__USER__ u', 'u.id=o.uid')
            ->where($where)
            ->field($fields)
            ->order($order)
            ->paginate($pageSize, false, ['query' => request()->get()]);
    }
}